import { api } from '@/utils/request'

const state = {
  moments: [],
  comments: [],
  currentMoment: null
}

const mutations = {
  SET_MOMENTS(state, moments) {
    state.moments = moments
  },
  SET_COMMENTS(state, comments) {
    state.comments = comments
  },
  ADD_COMMENT(state, comment) {
    state.comments.push(comment)
  },
  UPDATE_MOMENT(state, updatedMoment) {
    const index = state.moments.findIndex(moment => moment.id === updatedMoment.id);
    if (index !== -1) {
      // Use splice to ensure reactivity
      state.moments.splice(index, 1, updatedMoment);
    }
  }
}

const actions = {
  // 获取最新动态
  async fetchMoments({ commit }, params = { limit: 10 }) {
    try {
      const response = await api.get('/moments/recent', params)
      
      console.log('Moments API response:', response);
      const moments = response.data || [];
      console.log('Parsed moments:', moments);
      
      commit('SET_MOMENTS', moments)
      return moments
    } catch (error) {
      console.error('获取动态失败:', error)
      throw error
    }
  },

  // getRecentMoments 作为 fetchMoments 的别名, 保持兼容性
  async getRecentMoments({ dispatch }, params) {
    return dispatch('fetchMoments', params)
  },

  // 获取动态详情
  async getMomentDetail({ commit }, momentId) {
    try {
      const response = await api.get(`/moments/${momentId}`)
      
      return response.data
    } catch (error) {
      console.error('获取动态详情失败:', error)
      throw error
    }
  },
  
  async publishMoment({ commit }, momentData) {
    try {
        const response = await api.post('/moments/publish', momentData);
        // We don't add to state directly, the list page will refresh on 'onShow'
        return response.data;
    } catch (error) {
        console.error('发布动态失败:', error);
        throw error;
    }
  },

  async likeMoment({ commit }, momentId) {
    try {
        const response = await api.post(`/moments/${momentId}/like`);
        commit('UPDATE_MOMENT', response.data);
    } catch (error) {
        console.error('点赞动态失败:', error);
        throw error;
    }
  },
  
  async unlikeMoment({ commit }, momentId) {
    try {
        const response = await api.delete(`/moments/${momentId}/like`);
        commit('UPDATE_MOMENT', response.data);
    } catch (error) {
        console.error('取消点赞失败:', error);
        throw error;
    }
  },
  
  async fetchComments({ commit }, momentId) {
    try {
        const response = await api.get(`/moments/${momentId}/comments`);
        commit('SET_COMMENTS', response.data || []);
    } catch (error) {
        console.error('获取评论失败:', error);
    }
  },
  
  async addComment({ commit }, { momentId, commentData }) {
    try {
        const response = await api.post(`/moments/${momentId}/comments`, commentData);
        commit('ADD_COMMENT', response.data);
    } catch (error) {
        console.error('添加评论失败:', error);
        throw error;
    }
  }
}

const getters = {
  moments: state => state.moments,
  comments: state => state.comments,
  currentMoment: state => state.currentMoment
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
} 
